/*
 * @Date: 2024-04-02 16:05:22
 * @LastEditors: bingkui.wu
 * @LastEditTime: 2024-04-02 16:06:16
 * @Description: 获取教材和年级信息
 */
import { REQUEST } from "#type/request";
import Response from "@common/Response";
import { BookUnit } from "@models/BookUnit";
import { BookGrade } from "@models/BookGrade";
import { BookCategory } from "@models/BookCategory";
import { BookUnitWord } from "@models/BookUnitWord";

const root: REQUEST.BASIC = {
  auth: false,
  method: "get",
  request: async (ctx: any) => {
    const { gradeId, categoryId } = ctx.request.query;

    const units = await BookUnit.find({ gradeId })
      .lean()
      .select("-_id -__v")
      .exec();

    const category = await BookCategory.findOne({ categoryId }).lean().exec();
    const grade = await BookGrade.findOne({ gradeId }).lean().exec();
    const words = await BookUnitWord.find({gradeId}).lean().exec();

    return Response.Success(
      units.map((m) => ({
        ...m,
        ...{ gradeName: grade.gradeName, categoryName: category.categoryName, wordsCount: words.filter(w => m.unitId === w.unitId).length },
      }))
    );
  },
};

export default root;
